package com.ce.pms.customer.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.service.IAccountService;
import com.ce.pms.charge.service.IBankAccountService;
import com.ce.pms.charge.vo.BankAccountVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.PmsUtilityComparator;
import com.ce.pms.customer.dao.IClientDao;
import com.ce.pms.customer.dao.IClientRHouseDao;
import com.ce.pms.customer.dao.ICustomerDao;
import com.ce.pms.customer.dao.IHisResidentDao;
import com.ce.pms.customer.dao.IIdTypeDao;
import com.ce.pms.customer.dao.IIndividualDao;
import com.ce.pms.customer.dao.IInterestDao;
import com.ce.pms.customer.dao.IMemberDao;
import com.ce.pms.customer.dao.IOrganizationDao;
import com.ce.pms.customer.dao.IParticipantDao;
import com.ce.pms.customer.dao.IParticipantRelationDao;
import com.ce.pms.customer.dao.IParticipantRoleDao;
import com.ce.pms.customer.dao.IProRightDao;
import com.ce.pms.customer.dao.IRelationTypeDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ClientRHouseDO;
import com.ce.pms.customer.model.IdTypeDO;
import com.ce.pms.customer.model.IndividualDO;
import com.ce.pms.customer.model.InterestDO;
import com.ce.pms.customer.model.OrganizationDO;
import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.customer.model.ParticipantRelationDO;
import com.ce.pms.customer.model.ProRightDO;
import com.ce.pms.customer.model.RelationTypeDO;
import com.ce.pms.customer.model.ResidentInHisDO;
import com.ce.pms.customer.service.ICustomerService;
import com.ce.pms.customer.service.IProRightService;
import com.ce.pms.customer.vo.CustListVO;
import com.ce.pms.customer.vo.CustSearchVO;
import com.ce.pms.customer.vo.CustomerHouseVO;
import com.ce.pms.customer.vo.CustomerVO;
import com.ce.pms.customer.vo.IndividualVO;
import com.ce.pms.customer.vo.InterestVO;
import com.ce.pms.customer.vo.MemberVO;
import com.ce.pms.customer.vo.MoveVO;
import com.ce.pms.customer.vo.OrganizationVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IFlatDao;
import com.ce.pms.house.dao.IHouseDao;
import com.ce.pms.house.model.CoalitionHouseDomain;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.vo.HouseVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 功能描述:
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 付湘辉
 * Created on 2008-05-22
 */
public class CustomerServiceImpl extends PmsBaseServiceImpl implements
		ICustomerService {

	protected IAccountService accountService;

	protected IBankAccountService bankAccountService;

	protected IClientDao clientDao;

	protected IClientRHouseDao clientRHouseDao;

	protected ICustomerDao customerDao;
	protected IFlatDao flatDao;
	protected IHisResidentDao hisResidentDao;
	protected IHouseDao houseDao;
	protected IHouseService houseService;
	protected IIdTypeDao idTypeDao;
	protected IIndividualDao individualDao;

	/**
	 * 兴趣爱好DAO
	 */
	protected IInterestDao interestDao;

	protected IMemberDao memberDao;
	protected IOrganizationDao organizationDao;

	protected IParticipantRelationDao participantRelationDao;

	protected IParticipantRoleDao participantRoleDao;

	protected IProRightDao proRightDao;

	protected IProRightService proRightService;

	protected IRelationTypeDao relationTypeDao;

	private static final String LESSEE_RELATION = "lessee";
	private static final String MEMBER_RELATION = "member";
	private static final String OWNER_RELATION = "owner";


	public ClientDO addClient(ParticipantDO participantDO, SysrefcodeDO clientType,Long oprator){
		ClientDO clientDO= new ClientDO();
		clientDO.setParticipant(participantDO);
		clientDO.setClientType(clientType);
		clientDO.setSysInfo(super.createInfo(oprator));
		clientDO = this.clientDao.saveObject(clientDO);
		return clientDO;
	}

	/**
	 *
	 * 功能描述: 添加一个住户（包括业主和租户），在业务上，一个业主可以有多个住房，<br>
	 * 因此添加动作包括添加其住房信息和自身详细资料，对租户来说亦是如此，<br>
	 * 而一个业主或租户可以是一个个人或组织，组织类型的业主(租户)包含有一些个人信息，<br>
	 * 法人和委托人，在添加住户时，应同时添加这些信息及它们的关系信息。<br>
	 * 注意：此方法应被其子类重写以实现不同业务逻辑
	 *
	 * @author 付湘辉
	 * Created on:2008-6-10
	 *
	 * 修改原因：
	 * @param houseList 住户住房信息列表
	 * @param customerList 住户信息列表
	 * @roseuid 4828F65F01CD
	 * @return String "true" 表示成功  "false" 表示失败
	 */
	public String addCustomer(List<CustomerHouseVO> houseList, List<CustomerVO> customerList){
		return null;
	}

	public void addOrganizationCustomer() {
		// TODO Auto-generated method stub

	}

	/**
	 *
	 * 功能描述: 根据一个兴趣爱好id的拼装字符串，<br>
	 * 得到这些id所对应兴趣爱好的名称，将这些名称用逗号分隔符组装。
	 *
	 * @author 付湘辉
	 * Created on:2008-6-17
	 *
	 * 修改原因：
	 * @param interestIdString id的拼装字符串，以逗号分隔
	 * @return String 名称字符串
	 */
	public String assemblyInterestNameString(final String interestIdString){
		if (BlankUtil.isBlank(interestIdString)) {
			return "";
		}
		StringBuffer nameString = new StringBuffer();
		String idString = interestIdString;
		if (interestIdString.endsWith(SysInfo.SEPARATOR_COMMA)) {
			idString = idString.substring(0,idString.length()-1);
		}

		List<InterestDO> list = this.interestDao.getAllInterestList(idString);
		if (!BlankUtil.isBlank(list)) {
			for (InterestDO interestDO : list) {
				nameString.append(interestDO.getName());
				nameString.append(SysInfo.SEPARATOR_COMMA);
			}

			nameString = nameString.deleteCharAt(nameString.length()-1);
		}

		return nameString.toString();
	}

	public void customerLiveIn(MoveVO moveIn) {
		//客户入住
		Date moveInDate = DateUtil.convertStrToDate(moveIn.getMoveDate(), DateUtil.DEFAULT_SHORT_DATE_FORMAT);
		//获取业主、租户ID
		String clientIds = getClientIds(moveIn);
		String houseIds = getHouseIds(moveIn);
		StringBuffer hql = new StringBuffer("from ClientRHouseDO cr ");
		                        hql.append(" where cr.client.id IN(").append(clientIds).append(')');
		                        hql.append(" AND cr.house.keyword IN(").append(houseIds).append(')');
		//获取所有客户房屋关系DO, 入住
		List<ClientRHouseDO> results = this.clientRHouseDao.findAllResult(hql.toString(), new Object[]{});
		for(ClientRHouseDO cr : results) {
			cr.setInDate(moveInDate);
			cr.setOutDate(null);
		}

	}
	public void customerMoveOut(MoveVO moveOut) {

		//客户搬出
		SysInfo sysInfo = super.createInfo(moveOut.getCreator());
		Date moveOutDate = DateUtil.convertStrToDate(moveOut.getMoveDate(), DateUtil.DEFAULT_SHORT_DATE_FORMAT);
		SysrefcodeDO owner = this.sysrefcodeDao.getSysrefCodesByCode("CS_CLIENT_CLIENT_TYPE", ClientDO.CT_OWNER);
		SysrefcodeDO ownerMember = this.sysrefcodeDao.getSysrefCodesByCode("CS_CLIENT_CLIENT_TYPE", ClientDO.CT_OWNER_MEMBER);

		String clientGroup = getClientIds(moveOut);
		String houseIds = getHouseIds(moveOut);

		StringBuffer hql = new StringBuffer("FROM ClientRHouseDO cr ");
		                         hql.append(" WHERE cr.client.id IN(").append(clientGroup).append(')');
		                         hql.append(" AND cr.house.keyword IN(").append(houseIds).append(')');

		List<ClientRHouseDO> results = this.clientRHouseDao.findAllResult(hql.toString(), new Object[]{});

		for(ClientRHouseDO cr : results) {
			ResidentInHisDO his = new ResidentInHisDO(cr.getClient(),cr.getHouse(),sysInfo);
			his.setInDate(cr.getInDate());
			his.setOutDate(moveOutDate);
			this.hisResidentDao.doSave(his);
			if(cr.getClient().getClientType()==owner
					||cr.getClient().getClientType()==ownerMember) {
				//业主及其家庭成员不删除
				cr.setOutDate(moveOutDate);
			} else {
				//删除租户及成员与房屋关系
				cr.getClient().getClientRHouses().remove(cr);
				this.clientRHouseDao.deleteObject(cr);
			}
		}

	}
	/**
	 * Function:删除业主
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-13
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String deleteCustomer(Long participantId,String participantTypeCode,String customerTypeCode){
		// 如果是租户
		if (SysrefcodeDO.CLIENT_TYPE_LESSEE.equals(customerTypeCode)) {
			return this.deleteLesseeCustomer(participantId, participantTypeCode);
		// 如果是业主
		} else if (SysrefcodeDO.CLIENT_TYPE_OWNER.equals(customerTypeCode)) {
			return this.deleteOwnerCustomer(participantId, participantTypeCode);
		}

		return null;
	}

	/**
	 * Function:删除租户
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-13
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String deleteLesseeCustomer(Long participantId,String participantTypeCode){

		SysrefcodeDO customerTypeDo =
			this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE, SysrefcodeDO.CLIENT_TYPE_LESSEE);

		ClientDO clientDO = this.clientDao.queryClientByParticipantAndClientType(
				participantId, customerTypeDo.getId());

		//得到客户所有相关房屋
		List<CustomerHouseVO> houseList = this.getCustomerHouseListById(participantId,customerTypeDo);

		for (CustomerHouseVO customerHouseVO : houseList) {
			// 检查是否已合并
			checkYetMerge(customerHouseVO);

			divestOneClientRHouse(clientDO, customerHouseVO,0);
		}

		return null;
	}

	/**
	 * 方法描述: 根据客户和房屋删除家庭成员
	 * @author 胡煦
	 * Create on:2008-6-4
	 * @param participantId：参与人ID
	 * @param houseId：房屋ID
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Long deleteMemberRelation(Long participantId,Long houseId) {

		List memberList = this.findMemberBy(participantId, houseId);
		if(memberList != null){
			for(int i = 0;i< memberList.size() ; i++){
				MemberVO m = new MemberVO();
				m = (MemberVO) memberList.get(i);
				ParticipantDO participantDO = this.customerDao.findById(participantId);
				ParticipantRelationDO participantRelationDO = this.participantRelationDao.findById(m.getRelationId());
				participantDO.getForwardRelations().remove(participantRelationDO);
				ClientRHouseDO clientRHouseDO = this.clientRHouseDao.getClientRHouseByHouseAndClient(houseId, m.getClientId());
				ClientDO clientDO = this.clientDao.findById(m.getClientId());

				clientDO.getClientRHouses().remove(clientRHouseDO);
				//clientRHouseDO.setClient(null);
				this.clientRHouseDao.deleteObject(clientRHouseDO);
			}
		}

		return null;

	}

	/**
	 * Function:删除业主
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-13
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String deleteOwnerCustomer(Long participantId,String participantTypeCode){

		SysrefcodeDO customerTypeDo =
			this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE, SysrefcodeDO.CLIENT_TYPE_OWNER);

		ClientDO clientDO = this.clientDao.queryClientByParticipantAndClientType(
				participantId, customerTypeDo.getId());

		//得到客户所有相关房屋
		List<CustomerHouseVO> houseList = this.getCustomerHouseListById(participantId,customerTypeDo);

		for (CustomerHouseVO customerHouseVO : houseList) {
			// 检查是否已合并
			checkYetMerge(customerHouseVO);

			divestOneClientRHouse(clientDO, customerHouseVO,0);
		}

		return null;
	}

	public ClientDO editClient(ClientDO clientDO,ParticipantDO participantDO, SysrefcodeDO clientType,Long oprator){
		super.modifyInfo(clientDO.getSysInfo(), oprator);
		clientDO = this.clientDao.updateObject(clientDO);
		return clientDO;
	}

	/**
	 *
	 * 功能描述: 修改一个住户（包括业主和租户），在业务上，一个业主可以有多个住房，<br>
	 * 因此修改动作包括添加其住房信息和自身详细资料，对租户来说亦是如此，<br>
	 * 而一个业主或租户可以是一个个人或组织，组织类型的业主(租户)包含有一些个人信息，<br>
	 * 法人和委托人，在修改住户时，可能同时修改这些信息及它们的关系信息。<br>
	 * 注意：此方法应被其子类重写以实现不同业务逻辑
	 *
	 * @author 付湘辉
	 * Created on:2008-10-21
	 *
	 * 修改原因：
	 * @param houseList 住户住房信息列表
	 * @param customerList 住户信息列表
	 * @return String "true" 表示成功  "false" 表示失败
	 */
	public String editCustomer(List<CustomerHouseVO> houseList, List<CustomerVO> customerList){
		return null;
	}

	/**
	 *
	 * 功能描述: 根据房号查出基产权号和合同号，用一个CustomerHouseVO对象封装。
	 *
	 * @author 付湘辉
	 * Created on:2008-6-20
	 *
	 * 修改原因：
	 * @param houseNumber
	 * @return CustomerHouseVO 一个VO
	 */
	public CustomerHouseVO findCustomerHouseByHouseNumber(String houseNumber){

		this.checkIsNull(houseNumber);

		CustomerHouseVO customerHouseVO = new CustomerHouseVO();
		HouseDomain house = this.houseService.getHouseByNumber(houseNumber);

		ProRightDO proRight = this.proRightDao.getEffectiveProRightByHouse(house);
		if (!BlankUtil.isBlank(proRight)) {
			customerHouseVO.setHouseContractNumber(proRight.getContactNO());//合同号
			customerHouseVO.setHousePropertyNo(proRight.getProRightNO());//产权号
			// 如果是合并记账
			if (ProRightDO.CHARGE_SELECT_MERGER.equals(proRight.getChargeSelect())) {
				CoalitionHouseDomain cHouse = this.houseDao.getCoalition(house);
				proRight = this.proRightDao.getEffectiveProRightByHouseId(cHouse.getKeyword());
				if (BlankUtil.isBlank(proRight)) {
					return customerHouseVO;
				}
			}

			BankAccountVO baccvo = this.bankAccountService.getCurrentBankAccountByPro(proRight.getProRightId());
			if (!BlankUtil.isBlank(baccvo)) {
				customerHouseVO.setAccountOwner(baccvo.getAccOwner());
				customerHouseVO.setBankId(baccvo.getBankId());
				customerHouseVO.setBankName(baccvo.getBankName());
				customerHouseVO.setBankNumber(baccvo.getAccNumber());
				customerHouseVO.setContactNumber(baccvo.getContactNumber());
			}


		}
		CoalitionHouseDomain chouse = this.houseDao.getCoalition(house);
		customerHouseVO.setManageHouseId(chouse==null?null:chouse.getKeyword());
		return customerHouseVO;
	}

	/**
	 * 功能描述：根据参与人ID查询个人资料
	 *
	 * @author 付湘辉
	 * Created on 2008-05-29
	 *
	 * @param participantId
	 * @return IndividualVO
	 * @roseuid 4833C50D029F
	 */
	public IndividualVO findIndividualById(Long participantId) {
		if (BlankUtil.isBlank(participantId)) {
			return null;
		}
		IndividualDO individualDO = this.individualDao.findById(participantId);
		IndividualVO individualVO= this.copyIndividualDOToIndividualVO(individualDO);

		//设置兴趣爱好名称
		if (!BlankUtil.isBlank(individualVO)) {
			individualVO.setInterestNameStr(this.assemblyInterestNameString(individualDO.getInterest()));
		}

		return individualVO;
	}

	/**
	 * 方法描述: 根据选中户主ID及房屋找到其相应家庭成员
	 *
	 * @author 胡煦 Create on:2008-6-4
	 * @param id：户主ID
	 * @return 返回当前页的查询结果 Edit author: Edit on: 修改原因：
	 */
	public List<MemberVO> findMemberBy(Long id, Long hid) {
		List<MemberVO> memberList = new ArrayList<MemberVO>();
		Object[] obj = participantRoleDao.getParticipantRoleBy(id, hid);
		List list = memberDao.findMemberbyId((Long)obj[0], hid);
		for (int i = 0; i < list.size(); i++) {
			ClientRHouseDO clientRHouseDO =  (ClientRHouseDO) list.get(i);
			MemberVO memberVO = this.convertDoToVo(clientRHouseDO,id);
			memberList.add(memberVO);
			}
		return memberList;
	}

	/**
	 * 功能描述：根据参与人ID查询企业资料
	 *
	 * @author 付湘辉
	 * Created on 2008-05-29
	 *
	 * @param participantId
	 * @return OrganizationVO
	 */
	public OrganizationVO findOrganizationById(Long participantId) {
		if (BlankUtil.isBlank(participantId)) {
			return null;
		}
		OrganizationVO organizationVO = null;

		OrganizationDO organizationDO = this.organizationDao.findById(participantId);

		//得到法人
		IndividualDO corporationDO = this.participantRelationDao
				.getOrganizationParticipant(participantId,
						RelationTypeDO.RTYPE_ENTERPRISE_LEGAL);

		//得到委托/代理人
		IndividualDO consignerDO = this.participantRelationDao
		.getOrganizationParticipant(participantId,
				RelationTypeDO.RTYPE_ENTERPRISE_PROXY);

		organizationVO = this.copyDOToOrganizationVO(organizationDO,corporationDO,consignerDO);


		return organizationVO;
	}

	/**
	 * 功能描述: 根据企业名称查询企业资料，如果没有查到，返回一个空值 <br>
	 * @param participantName
	 * @return
	 *
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public OrganizationVO findOrganizationByName(String participantName) {
		super.checkParameterIsNull(participantName, "common:parameters_null_exception, ");
		OrganizationDO organizationDO = this.customerDao.queryOrganizationByName(participantName);
		if (BlankUtil.isBlank(organizationDO)) {
			return null;
		}
		OrganizationVO organizationVO = this.findOrganizationById(organizationDO.getParticipantId());

		return organizationVO;
	}

	/**
	 *
	 * 功能描述: 得到所有兴趣爱好VO的列表
	 *
	 * @author 付湘辉
	 * Created on:2008-5-30
	 *
	 * 修改原因：
	 * @return List<InterestVO> 兴趣爱好VO的列表
	 */
	public List<InterestVO> getAllInterestList(){
		List<InterestVO> resultList = null;
		InterestVO interestVO = null;
		List<InterestDO> list = this.interestDao.getAllInterestList();

		if(list != null){
			resultList = new ArrayList<InterestVO>();
			for (InterestDO interestDO : list) {
				interestVO = new InterestVO();
				interestVO.setInterestId(interestDO.getInterestId());
				interestVO.setName(interestDO.getName());
				interestVO.setCode(interestDO.getCode());
				interestVO.setValidity(interestDO.getValidity());

				resultList.add(interestVO);
			}
		}

		return resultList;
	}

	/***********************************************************************/
	/**
	 * Function: 根据参与人ID找到其房屋关系
	 * @param participantId
	 * @return
	 * Create author:ydw
	 * Create on:2008-6-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public List<CustomerHouseVO> getClientRHouseBy(Long participantId,String clientType)
	{
		SysrefcodeDO sysrefcodeDO = this.sysrefcodeDao.getSysrefCodesByCode("CS_CLIENT_CLIENT_TYPE", clientType);
		ClientDO client = this.clientDao.queryClientByParticipantAndClientType(participantId, sysrefcodeDO.getId());
		List<CustomerHouseVO> lists = new ArrayList<CustomerHouseVO>();

		if(!BlankUtil.isBlank(client))
		{

			List<ClientRHouseDO> list= this.clientRHouseDao.getClientRHouseListByClientId(client.getParticipantRoleId());
			if(ClientDO.CT_OWNER.equals(clientType))
			{
				for(ClientRHouseDO crhouse : list)
				{
					CustomerHouseVO houseVO = new CustomerHouseVO();
					HouseDomain house = crhouse.getHouse();
					houseVO.setHouseId(house.getKeyword());
					houseVO.setHouseNumber(house.getHouseNumber());
					houseVO.setOwnerName(client.getParticipant().getParticipantName());
					lists.add(houseVO);
				}
			}
			else if(ClientDO.CT_LESSEE.equals(clientType))
			{
				for(ClientRHouseDO crhouse : list)
				{
					CustomerHouseVO houseVO = new CustomerHouseVO();

					HouseDomain house = crhouse.getHouse();
					houseVO.setHouseId(house.getKeyword());
					String ownerName = this.houseService.getOwnerNameBy(house.getHouseNumber());
					houseVO.setHouseNumber(house.getHouseNumber());
					houseVO.setOwnerName(ownerName);
					lists.add(houseVO);
				}

			}
		}

		return lists;
	}

	/**
	 * 功能描述: 通过产权房号获得的该房间的管理房及其相关的产权房 <br>
	 * @param houseNumber 房号
	 * @return List<CustomerHouseVO> 住房信息列表
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-5-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<CustomerHouseVO> getCustomerHouseList(String houseNumber){

		super.checkParameterIsNull(houseNumber, "house:house_number_null_exception");

		List<CustomerHouseVO> chvoList = new ArrayList<CustomerHouseVO>();

		//实现逻辑
		// 1：调用房屋接口得到此房号对应的房屋信息
		// 2：利用HouseDomain 实体关系得到基产权共有的管理房信息，
		//    没有则跳过第3步，返回只包含它自己的列表
		// 3：得到此管理房下的所有产权房屋信息
		List<HouseDomain> houseList =houseService.getHouseDomainList(houseNumber);

        Set<Long> proRightShareHouseIds = new HashSet<Long>();

		CustomerHouseVO chvo = null;
		for (HouseDomain house : houseList) {
			chvo = new CustomerHouseVO();

			ProRightDO proright = proRightDao.getEffectiveProRightByHouse(house);

			chvo.setHouseId(house.getKeyword());//房屋ID
			chvo.setHouseNumber(house.getHouseNumber());//房号
			chvo.setHouseContractNumber(proright!=null?proright.getContactNO():"");//预售合同号
			chvo.setHousePropertyNo(proright!=null?proright.getProRightNO():"");//产权证号

			proRightShareHouseIds.add(house.getKeyword());

            // 获取划款银行帐号信息
			this.dealBankAccountInfo(chvo, proright);
			chvo.setChargeSelect(proright!=null?proright.getChargeSelect():"");
			CoalitionHouseDomain chouse = this.houseDao.getCoalition(house);
			chvo.setManageHouseId(chouse==null?null:chouse.getKeyword());
			chvoList.add(chvo);
		}

        if (SysrefcodeDO.CONSTANTS_ONE < chvoList.size()) {
            for (CustomerHouseVO customerHousevo : chvoList) {
                customerHousevo.setProRightShareHouseIds(new HashSet<Long>(proRightShareHouseIds));
            }
        }
//        Collections.sort(chvoList, PmsUtilityComparator.getComparator());
		return chvoList;
	}

	/**
	 * 功能描述: 通过参与人id得到其所有产权房屋信息VO对象列表。
     * 对于一个属于产权共有管理房的产权房来说，对应的VO对象的
     * proRightShareHouseIds属性将被赋值。 <br>
	 * @param participantId 参与人id
	 * @param clientType 客户类型code
	 * @return List<CustomerHouseVO> 房屋信息VO对象列表
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-6-11  <br>
	 * Edit author: 2008-10-23  <br>
	 * Edit on:付湘辉    <br>
	 * Why:  修改注释   <br>
	 */
	public List<CustomerHouseVO> getCustomerHouseListById(Long participantId,String clientType){
		checkParameterIsNull("",participantId,clientType);
		SysrefcodeDO clientTypeDo =
			this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE, clientType);
		return this.getCustomerHouseListById(participantId, clientTypeDo);
	}

	/**
	 * 功能描述: 通过参与人id得到其所有产权房屋信息VO对象列表。
     * 对于一个属于产权共有管理房的产权房来说，对应的VO对象的
     * proRightShareHouseIds属性将被赋值。 <br>
	 * @param participantId
	 * @param clientType
	 * @return List<CustomerHouseVO> 房屋信息VO对象列表
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-6-11  <br>
	 * Edit author: 2008-10-23  <br>
	 * Edit on:付湘辉    <br>
	 * Why:  修改注释   <br>
	 */
	public List<CustomerHouseVO> getCustomerHouseListById(Long participantId,SysrefcodeDO clientType){
        this.checkIsNull(participantId, clientType);

		//得到参与人实体
		ParticipantDO participantDO = this.customerDao.findById(participantId);
		//通过参与人和业主客户类型查询客户实体
		ClientDO client = this.clientDao.queryClientByParticipantAndClientType(participantDO, clientType);

		Set<ClientRHouseDO> clientRHouseSet = client.getClientRHouses();
        List<CustomerHouseVO> list = new ArrayList<CustomerHouseVO>();
		if (!BlankUtil.isBlank(clientRHouseSet)) {
			for (ClientRHouseDO clienthouseDO : clientRHouseSet) {
				if (SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(clienthouseDO.getHouse().getIsVirtual())) {
					continue;
				}
				list.add(this.assemblyDo2CustomerHouseVo(participantDO, clienthouseDO));
			}
		}
		Collections.sort(list, PmsUtilityComparator.getComparator());
		return list;
	}

	/**
	 * 功能描述: 根据房号查询住户房屋信息列表 <br>
	 * @param houseNumber
	 * @return
	 *
	 * Create author: <br>
	 * Create on:  <br>
	 * Edit author: 付湘辉 <br>
	 * Edit on: 2008-10-23   <br>
	 * Why:  加注释   <br>
	 */
	public List<CustomerHouseVO> getHouseAndCustomerBy(String houseNumber)
	{
		List<CustomerHouseVO> customerHouseList = this.houseService.getHouseAndCustomerBy(houseNumber);
		return customerHouseList;
	}

	/**
	 * Function:根据房间号获得房间的id及房号
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public HouseVO getHouseByHouseNumber(String houseNumber) {
		HouseDomain houseDO = this.houseService.getHouseByNumber(houseNumber);
		HouseVO house = null;
		if(!BlankUtil.isBlank(houseDO))
		{
			house = new HouseVO();
			house.setHouseNumber(houseDO.getHouseNumber());
			house.setKeyword(houseDO.getKeyword());
		}
		return house;
	}

	/**
	 * Function:<p>根据企业名称查询企业信息，法人信息，代理人信息，返回包括三个VO的对象数组</p>
	 * 			<p>数组的值为　1.企业 2.法人 3.代理人
	 * @param participantName
	 * @return
	 * Create author:罗军林
	 * Create on:2008-6-20
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public Object[] queryAllOrganization(String participantName){

		//企业信息
		OrganizationDO organizationDO=this.customerDao.queryOrganizationByName(participantName);
		//法人信息
		IndividualDO	legalIndividualDO=null;
		//代理人信息
		IndividualDO	proxyIndividualDO=null;

		if(organizationDO!=null && organizationDO.getParticipantId()!=null){
			legalIndividualDO=this.individualDao.queryIndividualDOByRelationTypeCode
								(organizationDO.getParticipantId(), RelationTypeDO.RTYPE_ENTERPRISE_LEGAL);
			proxyIndividualDO=this.individualDao.queryIndividualDOByRelationTypeCode
								(organizationDO.getParticipantId(), RelationTypeDO.RTYPE_ENTERPRISE_PROXY);
		}
		//DO->vo 转换
		OrganizationVO organizationVO =organizationDO==null ? null : this.convertOrganizationDOtoVO(organizationDO);
		IndividualVO legalIndividualVO=legalIndividualDO==null ? null : this.copyIndividualDOToIndividualVO(legalIndividualDO);
		IndividualVO proxyIndividualVO=proxyIndividualDO==null ? null : this.copyIndividualDOToIndividualVO(proxyIndividualDO);

		Object[] obj={organizationVO,legalIndividualVO,proxyIndividualVO};
		return obj;
	}

	/**
	 *
	 * 功能描述: 查询指定id的参与人和指定房间的关系，
	 * 查询出房间的所有客户列表，与指定的参与人id一一比较，如果相同则说明这个参与人与
	 * 该房间建立了客户关系，不论何种关系。
	 *
	 * @author 付湘辉
	 * Created on:2008-7-2
	 *
	 * 修改原因：
	 * @param participantId
	 * @param customerHouseVOList
	 * @return String 返回结果
	 * <ul>
	 * <li>null : 没有关系</li>
	 * <li>owner : 已经是这间房的业主</li>
	 * <li>lessee : 已经是这间房的租户</li>
	 * <li>member : 已经是这间房的业主的家庭成员</li>
	 * <li>member : 已经是这间房的租户的家庭成员</li>
	 * </ul>
	 */
	public String queryClientTypeByParticipantIdAhouse(Long participantId,CustomerHouseVO customerHouseVO){

		this.checkIsNull(participantId,customerHouseVO);

		//得到房屋信息
		HouseDomain houseDomain = this.houseService.getHouseByNumber(customerHouseVO.getHouseNumber());

		List<ClientRHouseDO> crhList = this.clientRHouseDao.getClientRHouseListByHouseId(houseDomain.getKeyword());
		ClientDO client = null;
		if (!BlankUtil.isBlank(crhList)) {
			for (ClientRHouseDO crhouseDO : crhList) {
				ClientDO temp = crhouseDO.getClient();
				if (!BlankUtil.isBlank(temp)) {
					Long tempPId = temp.getParticipant().getParticipantId();
					if (participantId.equals(tempPId)) {
						client = crhouseDO.getClient();
						break;
					}
				}
			}
		}

		if (!BlankUtil.isBlank(client)) {
			SysrefcodeDO clientType = client.getClientType();
			if (SysrefcodeDO.CLIENT_TYPE_OWNER.equals(clientType.getValue())) {
//				return SysrefcodeDO.CLIENT_TYPE_OWNER;
				return CustomerServiceImpl.OWNER_RELATION;
			}else if (SysrefcodeDO.CLIENT_TYPE_LESSEE.equals(clientType.getValue())){
//				return SysrefcodeDO.CLIENT_TYPE_LESSEE;
				return CustomerServiceImpl.LESSEE_RELATION;
			}else if(SysrefcodeDO.CLIENT_TYPE_OWNERMEMBER.equals(clientType.getValue())){
//				return SysrefcodeDO.CLIENT_TYPE_OWNERMEMBER;
				return CustomerServiceImpl.MEMBER_RELATION;
			}else if(SysrefcodeDO.CLIENT_TYPE_LESSEEMEMBER.equals(clientType.getValue())){
//				return SysrefcodeDO.CLIENT_TYPE_LESSEEMEMBER;
				return CustomerServiceImpl.MEMBER_RELATION;
			}
		}

		return null;
	}

	public MoveVO queryCustomerBy(Long clientId, Long houseId) {
		MoveVO retValue = new MoveVO();
		HouseDomain house = this.flatDao.findById(houseId);
		ClientDO client = this.clientDao.findById(clientId);
		SysrefcodeDO matchType = client.getClientType();//跟据业主还是租户只查相应的数据
		SysrefcodeDO ownerMember =   this.sysrefcodeDao.getSysrefCodesByCode("CS_CLIENT_CLIENT_TYPE", "003");
		SysrefcodeDO lesseeMember =  this.sysrefcodeDao.getSysrefCodesByCode("CS_CLIENT_CLIENT_TYPE", "004");

		//取得所有的多并房事
		StringBuffer houseGroup=new StringBuffer();
		CoalitionHouseDomain merge = this.flatDao.getMergeCoalition(house);
		if(!BlankUtil.isBlank(merge)) {
			for(HouseDomain h : merge.getHouses()) {
				retValue.getHouses().put(h.getKeyword(), h.getHouseNumber());
				houseGroup.append(h.getKeyword())
				.append(',');
			}
//			//去除尾部的','
			houseGroup.setLength(houseGroup.length() - 1);

		}else{
			retValue.getHouses().put(house.getKeyword(), house.getHouseNumber());
			houseGroup.append(house.getKeyword());
		}

		StringBuffer hql = new StringBuffer(" SELECT cr from ClientRHouseDO cr ");
								 hql.append(" inner join fetch cr.client cl ");
								 hql.append(" inner join fetch cl.participant party ");
								 hql.append(" where cr.validity='1' ");
								 hql.append(" AND cr.house.id in(");
								 hql.append(houseGroup).append(')');
								 hql.append(" order by cr.houseHolder");
		List<ClientRHouseDO> result = this.clientRHouseDao.findAllResult(hql.toString());
		for(ClientRHouseDO cr:result) {
			//入住时间
			retValue.setMoveDate(DateUtil.convertDateToStr(cr.getInDate(),DateUtil.DEFAULT_SHORT_DATE_FORMAT));
			//家庭成员
			if (cr.getClient().getClientType() == ownerMember
					||cr.getClient().getClientType() == lesseeMember) {
				if(cr.getHouseHolder()==null) {
					throw new BusinessException("数据异常，家庭成员没有建立业主.",BusinessException.ERROR);
				}
				if(cr.getHouseHolder().getClientType() == matchType) {
					MoveVO.Possessor possessor= retValue.getOwnerOrLessee().get(cr.getHouseHolder().getParticipantRoleId());
					//无则新建
					if(BlankUtil.isBlank(possessor)) {
						//业主名称
						possessor = retValue.new Possessor();
						//业主名称
						possessor.setName(cr.getHouseHolder().getParticipant().getParticipantName());
						//成员信息
						possessor.getMembers().put(cr.getClient().getParticipantRoleId(),
							cr.getClient().getParticipant().getParticipantName());

						retValue.getOwnerOrLessee().put(cr.getHouseHolder().getParticipantRoleId(),
							possessor);
//					}
					} else {
						//
						possessor.getMembers().put(cr.getClient().getParticipantRoleId(),
							cr.getClient().getParticipant().getParticipantName());
					}
				}
			} else if(cr.getClient().getClientType() == matchType){
				//业主
				MoveVO.Possessor possessor= retValue.getOwnerOrLessee().get(cr.getClient().getParticipantRoleId());
				if(BlankUtil.isBlank(possessor)) {
					//业主名称
					possessor = retValue.new Possessor();
					//业主名称
					possessor.setName(cr.getClient().getParticipant().getParticipantName());

					retValue.getOwnerOrLessee().put(cr.getClient().getParticipantRoleId(),
							possessor);

				}
			}
		}
		return retValue;
	}

	public IndividualDO queryCustomerByNumber(Long IdTypeID, String IdNumber) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 功能描述：根据条件查询出客户房屋信息
	 */

	public Page queryCustomerInfo(CustSearchVO vo, Integer pageNo, Integer pageSize) {

		if(BlankUtil.isBlank(vo.getHouseIds())){
		vo.setHouseIds( houseDao.getHouseIdByNumber(vo.getHouseNumber()) );
		}
		if(isNotFoundHouseByHouseNumber(vo) ){
			return new Page();
		}

		Page page=this.customerDao.queryCustomerInfo(vo, pageNo, pageSize);
		page.setResult( convertObjectCustomer(page.getResult()) );
		return page;
	}

	/**
	 * Function:根据企业名称查询企业信息。<br>
	 * 			将会查询这两个表：CS_PARTICIPANT,CS_ORGANIZATION<br>
	 * 			业务上决定企业名称数据的唯一性。如果数据不存在或者不止一个或者出现异常都将返回null<br>
	 * @param participantName
	 * @return
	 * Create author:罗军林
	 * Create on:2008-5-30
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public OrganizationVO queryOrganizationByName(String participantName) {
		OrganizationDO organizationDO=this.customerDao.queryOrganizationByName(participantName);
		if(organizationDO != null){
			return this.convertOrganizationDOtoVO(organizationDO);
		}
		return null;
	}
	/**
	 * Function:根据证件类型ID和证件号码查询参与人信息。<br>
	 * 			将会查询这三个表：CS_PARTICIPANT,CS_INDIVIDUAL,CS_ID_TYPE<br>
	 * 			业务上决定这两个条件决定数据的唯一性。如果数据不存在或者不止一个或者出现异常都将返回null<br>
	 * @param idTypeCode
	 * @param idNO
	 * @return
	 * Create author:罗军林
	 * Create on:2008-5-30
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public IndividualVO queryParticipant(String idTypeId, String idNO) {
		IndividualDO individualDO=this.customerDao.queryParticipant(idTypeId, idNO);
		//Do->VO转换
		if(individualDO != null){
			IndividualVO individualVO=this.copyIndividualDOToIndividualVO(individualDO);
			return individualVO;
		}else{
			return null;
		}

	}

	/**
	 * 功能描述: 查询指定了id的产权房的所有居住信息， <br>
	 * 如果该产权房属于双拼多拼房或是双拼(多拼)产权共有房，
	 * 那么相关产权房的居住信息同时查出，
	 * 只查询已入住但没有搬出日期的记录。 <br>
	 *
	 * @param vo 搜索条件，只需要houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-11-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<Object[]> queryResidentInfo(CustSearchVO vo) {
		checkIsNull(vo);
		checkIsNull(vo.getHouseId());
		//只查询已入住没搬出的信息
		vo.setOnlyInDate(true);

		vo.setHouseIds( houseDao.getHouseIdsById(vo.getHouseId()));

		return this.customerDao.queryResidentInfo(vo);
	}

	/**
	 * 功能描述：1.根据查询条件查出相应的业主和租户
	 *      2.如果参与人角色为家庭成员，则只查出相应的业主或租户并放在结果中。
	 *      3.查询结果中包括参与人名称，房号，住户类型，
	 *			个人的证件类型，证件号码（参与人是企业类型的则是法人代表的证件类型，证件号码），联系电话。并包括
	 *			参与人的ID,和房屋的ID
	 *     4.可对住户类型，姓名，房号排序
	 *     5.房号中可以输入管理房号如1-2AB，会查出1-2A和1-2B相关的住户纪录
	 *    　6.住户类型可以有　业主（个人）、业主（企业）、租户（个人）、租户（企业）;
	 * @param vo　封装的条件
	 * @param pageNo　　第几页
	 * @param pageSize　每页行数
	 * @return 对象数组　如下
	 * <p>　　房号　　　　姓名		　入住日期　搬出日期　 住户类型    证件编码 证件名 证件号               电话     参与人ID 房屋ID 客户房屋关系ID　客户ID</p>
	 * <p>　　1-2A　Jone sharped	2008-06-03　null   业主(个人)	    001	  身份证	450302197511081014	null	   9	   4       3           7  </p>
	 */
	public Page queryResidentInfo(CustSearchVO vo,
			Integer pageNo, Integer pageSize) {
		vo.setHouseIds( houseDao.getHouseIdByNumber(vo.getHouseNumber()) );
		if(isNotFoundHouseByHouseNumber(vo) ){
			return new Page();
		}
		return this.customerDao.queryResidentInfo(vo, pageNo, pageSize);
	}

	/**
	 * @param accountService the accountService to set
	 */
	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}

	/**
	 * @param bankAccountService the bankAccountService to set
	 */
	public void setBankAccountService(IBankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}


	public void setClientDao(IClientDao clientDao) {
		this.clientDao = clientDao;
	}
	public void setClientRHouseDao(IClientRHouseDao clientRHouseDao) {
		this.clientRHouseDao = clientRHouseDao;
	}

	public void setCustomerDao(ICustomerDao customerDao) {
		this.customerDao = customerDao;
	}


	public void setFlatDao(IFlatDao flatDao) {
		this.flatDao = flatDao;
	}

	public void setHisResidentDao(IHisResidentDao hisResidentDao) {
		this.hisResidentDao = hisResidentDao;
	}

	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	public void setIdTypeDao(IIdTypeDao idTypeDao) {
		this.idTypeDao = idTypeDao;
	}

	public void setIndividualDao(IIndividualDao individualDao) {
		this.individualDao = individualDao;
	}

	/**
	 *
	 * 功能描述: TODO
	 *
	 * @author 付湘辉
	 * Created on:2008-5-30
	 *
	 * 修改原因：
	 * @param interestDao
	 * @return void 返回结果
	 */
	public void setInterestDao(IInterestDao interestDao) {
		this.interestDao = interestDao;
	}


	/**
	 * @param memberDao the memberDao to set
	 */
	public void setMemberDao(IMemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public void setOrganizationDao(IOrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	/**
	 * @param participantDao the participantDao to set
	 */
	public void setParticipantDao(IParticipantDao participantDao) {
	}

	public void setParticipantRelationDao(
			IParticipantRelationDao participantRelationDao) {
		this.participantRelationDao = participantRelationDao;
	}

	public void setParticipantRoleDao(IParticipantRoleDao participantRoleDao) {
		this.participantRoleDao = participantRoleDao;
	}

	/**
	 * 功能描述: 设置产权Dao接口注入
	 *
	 * @author 付湘辉
	 * Created on:2008-6-10
	 *
	 * 修改原因：
	 * @param proRightDao the 产权Dao接口 to set
	 */
	public void setProRightDao(IProRightDao proRightDao) {
		this.proRightDao = proRightDao;
	}

	/**
	 * @param proRightService the proRightService to set
	 */
	public void setProRightService(IProRightService proRightService) {
		this.proRightService = proRightService;
	}

	public void setRelationTypeDao(IRelationTypeDao relationTypeDao) {
		this.relationTypeDao = relationTypeDao;
	}

	/**
	 *
	 * 功能描述: 验证指定id的参与人是不是指定房间的业主或租户或家庭成员，
	 * 查询出房间的所有客户列表，与指定的参与人id一一比较，如果相同则说明这个参与人与
	 * 该房间建立了客户关系，不论何种关系都抛出异常信息。
	 *
	 * @author 付湘辉
	 * Created on:2008-7-2
	 *
	 * 修改原因：
	 * @param participantId
	 * @param customerHouseVO
	 * @return String 返回空表示正常
	 */
	public String validateParticipantRoleInSameHouse(Long participantId,CustomerHouseVO customerHouseVO){

		String res = this.queryClientTypeByParticipantIdAhouse(participantId, customerHouseVO);
		ParticipantDO p = this.customerDao.findById(participantId);
		if (CustomerServiceImpl.OWNER_RELATION.equalsIgnoreCase(res)) {
			throw new BusinessException(
					"customer:customer_house_and_person_hasbeen_owner_exception,"
							+ p.getParticipantName()
							+ ":" + customerHouseVO.getHouseNumber(),
					BusinessException.WARNING);
		}else if (CustomerServiceImpl.LESSEE_RELATION.equalsIgnoreCase(res)){
			throw new BusinessException(
					"customer:customer_house_and_person_hasbeen_lessee_exception,"
							+ p.getParticipantName()
							+ ":" + customerHouseVO.getHouseNumber(),
					BusinessException.WARNING);
		}else if(CustomerServiceImpl.MEMBER_RELATION.equalsIgnoreCase(res)){
			throw new BusinessException(
					"customer:customer_house_and_person_hasbeen_member_exception,"
							+ p.getParticipantName()
							+ ":" + customerHouseVO.getHouseNumber(),
					BusinessException.WARNING);
		}/*else if(SysrefcodeDO.CLIENT_TYPE_LESSEEMEMBER.equals(res)){
			throw new BusinessException(
					"customer:customer_house_and_person_hasbeen_lesseemember_exception,"
							+ p.getParticipantName()
							+ ":" + customerHouseVO.getHouseNumber(),
					BusinessException.WARNING);
		}*/
		return null;
	}

	/**
	 *
	 * 功能描述: 增加或修改客户
	 *
	 * @author 许海堆
	 * Created on:2008-6-10
	 *
	 * 修改原因：
	 * @param participantDO 参与人实体
	 * @param clientType  客户类型实体
	 * @param oprator 操作用户
	 *
	 * @return ClientDO 客户实体
	 */
	protected ClientDO addOrEditClient(ParticipantDO participantDO, SysrefcodeDO clientType,Long oprator){
		ClientDO clientDO = this.clientDao.queryClientByParticipantAndClientType(participantDO.getParticipantId(), clientType.getId());

		if(BlankUtil.isBlank(clientDO)){
			return this.addClient(participantDO, clientType, oprator);
		}else{
			return this.editClient(clientDO, participantDO, clientType, oprator);
		}
	}

	/**
	 * Function:保存或者更新个人DO，如果存在个人的id存在，说明是更新，如果不存在，说明是增加
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-5
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected IndividualDO addOrEditIndividual(IndividualDO individualDO){

		if(!BlankUtil.isBlank(individualDO.getParticipantId())){
			IndividualDO tempIndividualD=this.individualDao.findById(individualDO.getParticipantId());
			SysInfo sysInfo = tempIndividualD.getSysInfo();
			PopulateUtil.populate(individualDO, tempIndividualD, null, null);

			super.modifyInfo(sysInfo, individualDO.getSysInfo() == null ? null: individualDO.getSysInfo().getCreator());
			tempIndividualD.setSysInfo(sysInfo);
			individualDO=this.individualDao.updateObject(tempIndividualD);

		} else {
			this.individualDao.saveObject(individualDO);
		}

		return individualDO;
	}

	/**
	 * Function:保存或者更新企业DO，如果存在企业的id存在，说明是更新，如果不存在，说明是增加
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-5
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected OrganizationDO addOrEditOrganization(OrganizationDO organizationDO) {
		if(!BlankUtil.isBlank(organizationDO.getParticipantId())){
			OrganizationDO tempOrganizationDO=this.organizationDao.findById(organizationDO.getParticipantId());

			if(!BlankUtil.isBlank(tempOrganizationDO)){
				SysInfo sysInfo = tempOrganizationDO.getSysInfo();
				PopulateUtil.populate(organizationDO, tempOrganizationDO, null, null);
				super.modifyInfo(sysInfo, organizationDO.getSysInfo() == null ? null
						: organizationDO.getSysInfo().getCreator());
				tempOrganizationDO.setSysInfo(sysInfo);
				organizationDO=this.organizationDao.updateObject(tempOrganizationDO);
			}

		} else {
			this.organizationDao.saveObject(organizationDO);
		}

		return organizationDO;
	}

	/**
	 * Function:保存或者更新参与人直接的关系
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected ParticipantRelationDO addOrEditParticipantRelation(ParticipantDO organizationDO, ParticipantDO participant2, String relationTypeCode,Long oprator ){

		RelationTypeDO relationTypeDO = this.relationTypeDao.getRelationTypeByCode(relationTypeCode);

		ParticipantRelationDO participantRelation = this.participantRelationDao.queryParticipantRelation(organizationDO.getParticipantId(), participant2.getParticipantId(), relationTypeDO.getRelationTypeId());

		if(BlankUtil.isBlank(participantRelation)) {

			participantRelation = new ParticipantRelationDO();
			participantRelation.setParticipant1(organizationDO);
			participantRelation.setParticipant2(participant2);

			participantRelation.setRelationType(relationTypeDO);
			SysInfo sysInfo = super.createInfo(oprator);
			participantRelation.setSysInfo(sysInfo);
			this.participantRelationDao.saveObject(participantRelation);

		}else{
			SysInfo sysInfo = participantRelation.getSysInfo();
			super.modifyInfo(sysInfo, oprator);
			this.participantRelationDao.updateObject(participantRelation);
		}

		return participantRelation;
	}

	/**
	 * 功能描述: 检查参数是否为空
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param participantId
	 * @param clientType
	 */
	protected void checkIsNull(Object...objs) {
		super.checkParameterIsNull("common:parameters_null_exception, ", objs);
	}

	/** 功能描述:  <br>
	 * @param clientDO
	 * @param customerHouseVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	protected Boolean checkOwnerIsOnly(Long participantId,CustomerHouseVO customerHouseVO,int curAddOwnerCount) {
		List<ParticipantDO> list = this.clientRHouseDao.getAllOwnerByHouseId(customerHouseVO.getHouseId());
		if (SysrefcodeDO.CONSTANTS_ONE < list.size()-curAddOwnerCount) {
//			throw new BusinessException("customer:owner_lastowner_cannotdel_inhouse_exception,"
//								+ participantName
//								+":"
//								+customerHouseVO.getHouseNumber(),
//							BusinessException.WARNING);
			return false;
		}else if(SysrefcodeDO.CONSTANTS_ONE == list.size()-curAddOwnerCount){
			for (ParticipantDO participantDO : list) {
				if (participantDO.getParticipantId().equals(participantId)) {
					return true;
				}
			}
		}

		return false;

	}

	/** 功能描述:  <br>
	 * @param clientDO
	 * @param customerHouseVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	protected void checkUpCharge(ClientDO clientDO, CustomerHouseVO customerHouseVO) {
		if (this.queryIsProduceCharge(customerHouseVO.getHouseId())) {
			// 已存在费用
			throw new BusinessException("customer:owner_house_has_produced_charges,"
					+clientDO.getParticipant().getParticipantName()
					+":"
					+customerHouseVO.getHouseNumber(),
				BusinessException.WARNING);
		}
	}

	/** 功能描述: 检查一间产权房是否已和另外产权房合并。 <br>
	 * @param customerHouseVO 住户房屋信息
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	protected void checkYetMerge(CustomerHouseVO customerHouseVO) {

		CoalitionHouseDomain cHouse = this.houseDao.getCoalition(customerHouseVO.getHouseId());
		if (!BlankUtil.isBlank(cHouse)) {
			throw new BusinessException("customer:customer_house_has_coal_notdel_exception,"
					+customerHouseVO.getHouseNumber(),
				BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: DO to VO
	 * @author 胡煦
	 * Create on:2008-7-14
	 * @param ClientDO,participantId
	 * @return MemberVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	protected MemberVO convertDoToVo(ClientRHouseDO clientRHouseDO,Long participantId){
		MemberVO memberVO = new MemberVO();
		memberVO.setOwnerId(participantId);
		memberVO.setHouseId(clientRHouseDO.getHouse() == null ? null :clientRHouseDO.getHouse().getKeyword());
		memberVO.setHouseNumber(clientRHouseDO.getHouse() == null ? null :clientRHouseDO.getHouse().getHouseNumber());
		ClientDO clientDO = clientRHouseDO.getClient();
		memberVO.setOwnerName(clientRHouseDO.getHouseHolder().getParticipant().getParticipantName());
		ParticipantDO participantDO = clientDO.getParticipant();
		//得到成员Id
		Long inId = participantDO.getParticipantId();
		//得到关系类型DO
		ParticipantRelationDO participantRelationDO = this.participantRelationDao.queryParticipantRelation(participantId, inId);
		//得到个人DO
		IndividualDO individualDO = this.individualDao.findById(inId);
		memberVO.setCertificateNo(individualDO.getIdNumber());
		//判断是个人还是企业
		if(!BlankUtil.isBlank(individualDO)){
			PopulateUtil.populate(individualDO, memberVO);
			memberVO.setID_typeId(individualDO.getIdType().getIdTypeId());
			memberVO.setID_type(individualDO.getIdType().getIdTypeName());
			//设置兴趣爱好名称
			memberVO.setInterestNameStr(this
					.assemblyInterestNameString(individualDO.getInterest()));
			memberVO.setSexId(individualDO.getSex().getId());
			memberVO.setSex(individualDO.getSex().getName());
			memberVO.setSysDate(DateUtil.convertDateToStr(individualDO
					.getSysInfo().getSysDate(), "yyyy-MM-dd"));

		}else{
			OrganizationDO organizationDO = this.organizationDao.findById(inId);
			PopulateUtil.populate(organizationDO, memberVO);
		}
		memberVO.setClientId(clientDO.getParticipantRoleId());

		memberVO.setRelationId(participantRelationDO== null ? null :
					participantRelationDO.getParticipantRelationId());
		memberVO.setRelation(participantRelationDO== null ? null :participantRelationDO.getRelationType()
				.getRelationTypeName());
		memberVO.setRelationTypeId(participantRelationDO== null ? null :participantRelationDO.getRelationType()
				.getRelationTypeId());
		memberVO.setInDate(DateUtil.convertDateToStr(clientRHouseDO.getInDate(),
				"yyyy-MM-dd"));
		memberVO.setOutDate(DateUtil.convertDateToStr(
				clientRHouseDO.getOutDate(), "yyyy-MM-dd"));

		return memberVO;
	}

	/**
	 *
	 * 功能描述: 拷贝一个组织持久化实体对象到VO对象，包括这个VO对象所包含的法人和代理人实体
	 *
	 * @author 付湘辉
	 * Created on:2008-6-17
	 *
	 * 修改原因：
	 * @param organizationDO
	 * @param corporationDO
	 * @param consignerDO
	 * @return OrganizationVO 返回结果
	 */
	protected OrganizationVO copyDOToOrganizationVO(OrganizationDO organizationDO, IndividualDO corporationDO, IndividualDO consignerDO){
		OrganizationVO organizationVO = null;

		if(!BlankUtil.isBlank(organizationDO)){

			organizationVO = new OrganizationVO();
			organizationVO.setParticipantId(organizationDO.getParticipantId());//（参与人ID）组织id
			organizationVO.setParticipantName(organizationDO.getParticipantName());//企业名称
			organizationVO.setTelephone(organizationDO.getTelephone());//联系电话
			organizationVO.setAddress(organizationDO.getAddress());//企业地址
			organizationVO.setZip(organizationDO.getZipcode());//邮编
			organizationVO.setOrganizationLinkMan(organizationDO.getContactor());//联系人

			//		设置法人信息
			if (!BlankUtil.isBlank(corporationDO)) {
				organizationVO.setCorporationId(corporationDO.getParticipantId());//id（法人）
				organizationVO.setOrganizationPersonName(corporationDO.getParticipantName());//法人姓名
				organizationVO.setCertificateType(corporationDO.getIdType().getIdTypeId());//法人证件类型id
				organizationVO.setCertificateTypeCode(corporationDO.getIdType().getIdTypeCode());//法人证件类型编号
				organizationVO.setCertificateTypeName(corporationDO.getIdType().getIdTypeName());//法人证件类型名称
				organizationVO.setCertificateNo(corporationDO.getIdNumber());//法人证件号码
				organizationVO.setAppellative(corporationDO.getAppellative());//法人称呼
				organizationVO.setSex(corporationDO.getSex().getId());//法人性别id
				organizationVO.setSexDisplayValue(corporationDO.getSex().getName());//法人性别显示值
				organizationVO.setOrganizationPersonPhone(corporationDO.getTelephone());//法人联系电话
				organizationVO.setCorporationBirthDate(corporationDO.getBirthday());//出生日期（法人）
				organizationVO.setCorporationBirthDateDisplayValue(DateUtil.convertDateToStr(corporationDO.getBirthday(), DateUtil.DEFAULT_SHORT_DATE_FORMAT));
				organizationVO.setRegisterPlace(corporationDO.getRegisterPlace());//户口所在地（法人）
				organizationVO.setLinkAddress(corporationDO.getContactAddress());//联系地址（法人）
				organizationVO.setEmail(corporationDO.getEmail());//联系邮件（法人）
				organizationVO.setRemark(corporationDO.getRemark());//备注（法人）
			}

			//设置委托人/代理人信息
			if (!BlankUtil.isBlank(consignerDO)) {
				organizationVO.setConsignerId(consignerDO.getParticipantId());//id（代理人）
				organizationVO.setConsignerName(consignerDO.getParticipantName());//姓名（代理人）
				organizationVO.setConsignerSex(consignerDO.getSex().getId());//性别（代理人）id
				organizationVO.setConsignerSexDisplayValue(consignerDO.getSex().getName());//性别显示值（代理人）
				organizationVO.setConsignerCertificateType(consignerDO.getIdType().getIdTypeId());//证件类型（代理人）
				organizationVO.setConsignerCertificateTypeCode(consignerDO.getIdType().getIdTypeCode());//证件类型编号（代理人）
				organizationVO.setConsignerCertificateTypeName(consignerDO.getIdType().getIdTypeName());//证件类型名称（代理人）
				organizationVO.setConsignerCertificateNo(consignerDO.getIdNumber());//证件号码（代理人）
				organizationVO.setConsignerAddress(consignerDO.getContactAddress());//联系地址（代理人）
				organizationVO.setConsignerEmail(consignerDO.getEmail());//电子邮件（代理人）
				organizationVO.setConsignerPhone(consignerDO.getTelephone());//联系电话（代理人）
				organizationVO.setConsignerHomeplace(consignerDO.getHomePlace());//籍贯（代理人）
			}
		}

		return organizationVO;
	}

	/**
	 * Function:individualDO转换为IndividualVO
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected IndividualVO copyIndividualDOToIndividualVO(IndividualDO individualDO){

		IndividualVO individualVO = null;
		if(!BlankUtil.isBlank(individualDO)) {
			individualVO=new IndividualVO();
			individualVO.setParticipantId(individualDO.getParticipantId());
			individualVO.setAppellative(individualDO.getAppellative());//称呼
			individualVO.setInterest(individualDO.getInterest());//兴趣爱好
			individualVO.setParticipantId(individualDO.getParticipantId());//参与人ID
			individualVO.setParticipantName(individualDO.getParticipantName());//参与人名称
			individualVO.setSex(individualDO.getSex().getId());//性别id
			individualVO.setSexDisplayValue(individualDO.getSex().getName());//性别显示值
			individualVO.setTelephone(individualDO.getTelephone());//联系电话
			individualVO.setBirthDate(individualDO.getBirthday());//出生日期
			individualVO.setBirthDateDisplayValue(DateUtil.convertDateToStr(individualDO.getBirthday(), DateUtil.DEFAULT_SHORT_DATE_FORMAT));
			individualVO.setCertificateType(individualDO.getIdType().getIdTypeId());//证件类型ＩＤ
			individualVO.setCertificateNo(individualDO.getIdNumber());//证件号码
			individualVO.setCertificateTypeCode(individualDO.getIdType().getIdTypeCode());//证件类型编号
			individualVO.setCertificateTypeName(individualDO.getIdType().getIdTypeName());//证件类型名称
			individualVO.setCorporation(individualDO.getCorporation());//单位名称
			individualVO.setEmail(individualDO.getEmail());//邮件
			individualVO.setEmergencyMan(individualDO.getEmergencyMan());//紧急联系人
			individualVO.setEmergencyPhone(individualDO.getEmergencyPhone());//紧急联系电话
			individualVO.setHomeplace(individualDO.getHomePlace());//设置籍贯
			individualVO.setLinkAddress(individualDO.getContactAddress());//联系地址
			individualVO.setProfession(individualDO.getProfession());//职业
			individualVO.setRegisterCar(individualDO.getRegisterCar());//车辆状况
			individualVO.setRegisterPlace(individualDO.getRegisterPlace());//户口所在地
			individualVO.setRemark(individualDO.getRemark());//备注
			individualVO.setParticipantTypeCode(CustomerVO.INDIVIDUAL_CODE);
		}

		return individualVO;
	}

	/**
	 * Function:vo转换为do之后对个人资料进行保存
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected IndividualDO dealIndividual(CustomerVO customerVO) {
		IndividualVO individualVO = (IndividualVO)customerVO;
		IndividualDO individualDO = new IndividualDO();
		PopulateUtil.populate(individualVO, individualDO, null, null);

		SysInfo sysInfo = super.createInfo(individualVO.getCreator());
		individualDO.setSysInfo(sysInfo);
		SysrefcodeDO sex = new SysrefcodeDO();
		sex.setId(individualVO.getSex());
		individualDO.setSex(sex);//性别
		IdTypeDO idType = new IdTypeDO();
		idType.setIdTypeId(individualVO.getCertificateType());
		individualDO.setIdType(idType);//证件类型
		individualDO.setIdNumber(individualVO.getCertificateNo());//证件号码
		individualDO.setContactAddress(individualVO.getLinkAddress());//联系地址
		individualDO.setBirthday(individualVO.getBirthDate());//出生日期
		individualDO.setHomePlace(individualVO.getHomeplace());//籍贯

		//新增或修改信息
		individualDO = this.addOrEditIndividual(individualDO);
		//把id返回去
		customerVO.setParticipantId(individualDO.getParticipantId());

		return individualDO;
	}

	/**
	 * Function:vo转换为do之后对企业资料进行保存，同时保存法人资料、代理人资料、法人和企业之间的关系、代理人和企业之间的关系
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected OrganizationDO dealOrgnization(CustomerVO customerVO) {

		OrganizationVO organizationVO = (OrganizationVO)customerVO;
        //企业
		OrganizationDO organizationDO = new OrganizationDO();
		PopulateUtil.populate(organizationVO, organizationDO, null, null);
		organizationDO.setContactor(organizationVO.getOrganizationLinkMan());//联系人
		organizationDO.setZipcode(organizationVO.getZip());//邮政编码
		SysrefcodeDO organizationType =this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CS_ORGANIZATION_ORGANIZATION_TYPE_CODE,SysrefcodeDO.CS_ORGANIZATION_ORGANIZATION_TYPE_VALUE_OTHER);
		organizationDO.setOrganizationType(organizationType);
		Long oprator = organizationVO.getCreator();
		SysInfo sysInfo = super.createInfo(oprator);
		organizationDO.setSysInfo(sysInfo);
		//保存企业的信息
		organizationDO = this.addOrEditOrganization(organizationDO);
		//把id返回去
		organizationVO.setParticipantId(organizationDO.getParticipantId());

		//法人代表
		this.dealArtificialPerson(organizationVO, organizationDO, oprator, sysInfo);

		//代理人
		this.dealConsigner(organizationVO, organizationDO, oprator, sysInfo);

		return organizationDO;

	}

	/**
	 * Function:根据参与人和参与人的类型code，删除对应参与人关系
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected void deleteParticipantRelation(Long participantId1, Long participantId2, String relationTypeCode) {

		RelationTypeDO relationTypeDO = this.relationTypeDao.getRelationTypeByCode(relationTypeCode);

		ParticipantRelationDO participantRelation = this.participantRelationDao.queryParticipantRelation(participantId1, participantId2, relationTypeDO.getRelationTypeId());

		if(!BlankUtil.isBlank(participantRelation)) {
			this.participantRelationDao.deleteObject(participantRelation);
		}
	}

	/**
	 *
	 * 功能描述: 解除住户房屋的关系
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param customerHouseVO
	 * @param clientDO
	 * @return String
	 */
	@Deprecated
	protected String divestClientRHouse(CustomerHouseVO customerHouseVO, ClientDO clientDO) {
		ClientRHouseDO clientRHouseDO
		   = this.clientRHouseDao.getClientRHouseByHouseAndClient(customerHouseVO.getHouseId(), clientDO.getParticipantRoleId());

		if (!BlankUtil.isBlank(clientRHouseDO)) {
			clientDO.getClientRHouses().remove(clientRHouseDO);
			this.clientRHouseDao.deleteObjectById(clientRHouseDO.getClientRHouseId());
		}

		return "true";
	}

	/** 功能描述: 解除一个客户与房屋的关系，
	 * 客户的家庭成员一并删除 <br>
	 * @param clientDO
	 * @param customerHouseVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	protected void divestOneClientRHouse(ClientDO clientDO, CustomerHouseVO customerHouseVO,int curAddOwnerCount) {
		ClientRHouseDO clientRHouseDO = this.clientRHouseDao
				.getClientRHouseByHouseAndClient(customerHouseVO.getHouseId(), clientDO.getParticipantRoleId());

		if (!BlankUtil.isBlank(clientRHouseDO)) {
//			检查是否是唯一的业主
			Boolean isOnlyOwner = false;
			if (ClientDO.CT_OWNER.equals(clientDO.getClientType().getValue())) {
				isOnlyOwner = this.checkOwnerIsOnly(clientDO.getParticipant().getParticipantId(), customerHouseVO,curAddOwnerCount);
			}

			//解除该住户所有家庭成员与房间的关系
			this.deleteMemberRelation(clientDO.getParticipant().getParticipantId(), customerHouseVO.getHouseId());

			// 执行删除动作：解除客户与房屋的关系
			if(!BlankUtil.isBlank(clientDO.getClientRHouses())){
				clientDO.getClientRHouses().remove(clientRHouseDO);
			}

			this.clientRHouseDao.deleteObjectById(clientRHouseDO.getClientRHouseId());

			if (isOnlyOwner
					&& ClientDO.CT_OWNER.equals(clientDO.getClientType().getValue())) {
				// 如果业主的任一房间发生任何交易费用，
				// 则不能删除该业主
				checkUpCharge(clientDO, customerHouseVO);

				// 如果已建账户，则应物理删掉此账户
				this.accountService.deleteAccountByHouseId(customerHouseVO.getHouseId());
				// 银行帐号的处理，物理删除
				this.bankAccountService.deleteBankAccount(customerHouseVO.getHouseId());
				// 对产权的处理，物理删除
				this.proRightService.deleteEffectiveProRight(customerHouseVO.getHouseId());
			}
		}
	}

	/**
	 *
	 * 功能描述: 查询指定房屋是否已产生费用
	 * <b>查询规则</b>：通过房屋id查询到实体，再
	 * 查询到这间房屋的总账户，对总账户下
	 * 的交易记录进行查询，有记录说明已产生了费用。<br>
	 *
	 * @author 付湘辉
	 * Created on:2008-6-16
	 *
	 * 修改原因：
	 * @param houseId 房屋id
	 * @return boolean 返回<b>true</b>表示已存在费用
	 */
	protected boolean queryIsProduceCharge(Long houseId){
		super.checkParameterIsNull(houseId, "common:parameters_null_exception,ID");
		HouseDomain house = this.houseDao.findById(houseId);
//		List<ParticipantDO> list = this.clientRHouseDao.getAllOwnerByHouseId(houseId);
//		if (SysrefcodeDO.CONSTANTS_ONE < list.size()) {
//			return false;
//		}

		return this.accountService.queryHouseIsProduceCharge(house);
	}

	/**
	 * 功能描述: 装配一个客户住户VO对象,如果对应的房屋是产权共有管理房下的一员，
     * 则会对这个VO的proRightShareHouseIds进行赋值
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param participantDO
	 * @param clienthouseDO
	 * @return CustomerHouseVO
	 */
	private CustomerHouseVO assemblyDo2CustomerHouseVo(ParticipantDO participantDO, ClientRHouseDO clienthouseDO) {
		CustomerHouseVO customerHouseVO = new CustomerHouseVO();

		// 处理房屋相关
        dealHouseInfo(clienthouseDO, customerHouseVO);

        ProRightDO proRight = clienthouseDO.getProRight();
		if (!BlankUtil.isBlank(proRight)) {
			customerHouseVO.setHouseContractNumber(proRight.getContactNO());//合同号
			customerHouseVO.setHousePropertyNo(proRight.getProRightNO());//产权证号
		}

		customerHouseVO.setOwnerName(participantDO.getParticipantName());//业主姓名(查询租户时使用)

		// 获取划款银行帐号信息
		this.dealBankAccountInfo(customerHouseVO, proRight);
		customerHouseVO.setChargeSelect(proRight!=null?proRight.getChargeSelect():"");
		return customerHouseVO;
	}

	/**
	 * 功能描述: 装配一个法人代表个人DO实体
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param organizationVO
	 * @param sysInfo
	 * @return
	 */
	private IndividualDO assemblyVo2ArtificialPersonDO(OrganizationVO organizationVO, SysInfo sysInfo) {
		IndividualDO artificialPersonDO = new IndividualDO();
		//如果法人id不为空，则查询其信息
		if (!BlankUtil.isBlank(organizationVO.getCorporationId())) {
			artificialPersonDO = this.individualDao.findById(organizationVO.getCorporationId());
		}

		artificialPersonDO.setSysInfo(sysInfo);//创建人信息
		artificialPersonDO.setParticipantName(organizationVO.getOrganizationPersonName());//姓名
		artificialPersonDO.setAppellative(organizationVO.getAppellative());//称呼
		SysrefcodeDO sex = new SysrefcodeDO();
		sex.setId(organizationVO.getSex());
		artificialPersonDO.setSex(sex);//性别
		artificialPersonDO.setTelephone(organizationVO.getOrganizationPersonPhone());
		IdTypeDO idType = new IdTypeDO();
		idType.setIdTypeId(organizationVO.getCertificateType());
		artificialPersonDO.setIdType(idType);//证件类型
		artificialPersonDO.setIdNumber(organizationVO.getCertificateNo());//证件号码

		artificialPersonDO.setBirthday(organizationVO.getCorporationBirthDate());//出生日期（法人）
		artificialPersonDO.setRegisterPlace(organizationVO.getRegisterPlace());//户口所在地
		artificialPersonDO.setContactAddress(organizationVO.getLinkAddress());//联系地址
		artificialPersonDO.setEmail(organizationVO.getEmail());//电子邮件
		artificialPersonDO.setRemark(organizationVO.getRemark());//备注

		return artificialPersonDO;
	}

	/**
	 * 功能描述: TODO
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param organizationVO
	 * @param sysInfo
	 * @return IndividualDO
	 */
	private IndividualDO assemblyVO2ConsignerDO(OrganizationVO organizationVO, SysInfo sysInfo) {
		//代理人
		IndividualDO agentDO = new IndividualDO();
		//如果有id，查询其信息
		if (!BlankUtil.isBlank(organizationVO.getConsignerId())) {
			agentDO = this.individualDao.findById(organizationVO.getConsignerId());
		}

		agentDO.setSysInfo(sysInfo);//创建人信息
		agentDO.setParticipantId(organizationVO.getConsignerId());

		agentDO.setParticipantName(organizationVO.getConsignerName());
		SysrefcodeDO sexAgent = new SysrefcodeDO();
		sexAgent.setId(organizationVO.getConsignerSex());
		agentDO.setSex(sexAgent);
		IdTypeDO idTypeAgent = new IdTypeDO();
		idTypeAgent.setIdTypeId(organizationVO.getConsignerCertificateType());
		agentDO.setIdType(idTypeAgent);//证件类型
		agentDO.setIdNumber(organizationVO.getConsignerCertificateNo());//证件号码
		agentDO.setContactAddress(organizationVO.getConsignerAddress());//联系地址
		agentDO.setEmail(organizationVO.getConsignerEmail());//电子邮件
		agentDO.setTelephone(organizationVO.getConsignerPhone());//联系电话
		agentDO.setHomePlace(organizationVO.getConsignerHomeplace());//籍贯

		return agentDO;
	}

	/**
	 * Function:页面展示数据的组装
	 * @param customerList
	 * @return
	 * Create author:罗军林
	 * Create on:2008-5-28
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private List<CustListVO> convertObjectCustomer(List customerList) {
		List<CustListVO> tempList = new ArrayList<CustListVO>();
		for (Iterator<Object[]> it = customerList.iterator(); it.hasNext();) {
			Object[] item = it.next();
			CustListVO tempVO = new CustListVO();
			tempVO.setCustomerName( (String)item[0] );
			tempVO.setHouseNumber( (String)item[1] );
			tempVO.setCustomerType( (String)item[2] );
			tempVO.setIdType( (String)item[3] );
			tempVO.setIdNO( (String)item[4] );
			tempVO.setTelephone( (String)item[5] );
			tempVO.setClientID( item[7]==null ? null : String.valueOf(item[7]) );
			tempVO.setCustomerTypeCode( (String)item[8] );
			tempVO.setIndividualID(  item[9]==null ? null : String.valueOf(item[9]) );
			tempVO.setParticipantTypeCode( (String)item[10] );
			tempVO.setProRightId( item[11]==null ? null : ((BigInteger)item[11]).longValue() );
			tempVO.setBankAccountId( item[12]==null ? null :((BigInteger)item[12]).longValue() );
			tempVO.setHouseId(  item[13]==null ? null : String.valueOf(item[13]) );
			tempList.add(tempVO);
		}
		return tempList;
	}
	/**
	 * Function:将企业信息的DO转换成页面需要的VO
	 * @param organizationDO
	 * Create author:罗军林
	 * Create on:2008-6-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private OrganizationVO convertOrganizationDOtoVO(OrganizationDO organizationDO) {
		OrganizationVO organizationVO=new OrganizationVO();
		organizationVO.setParticipantId(organizationDO.getParticipantId());//企业ID
		organizationVO.setParticipantName(organizationDO.getParticipantName());//企业名称
		organizationVO.setTelephone(organizationDO.getTelephone());//联系电话
		organizationVO.setZip(organizationDO.getZipcode());//邮编
		organizationVO.setOrganizationLinkMan(organizationDO.getContactor());//联系人
		organizationVO.setAddress(organizationDO.getAddress());//企业地址
		if(organizationDO.getOrganizationType()!=null){
			organizationVO.setParticipantTypeCode(organizationDO.getOrganizationType().getValue());//企业类型
		}
		return organizationVO;
	}

	/**
	 * 功能描述: 法人代表信息处理
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param organizationVO
	 * @param organizationDO
	 * @param oprator
	 * @param sysInfo
	 */
	private void dealArtificialPerson(OrganizationVO organizationVO, OrganizationDO organizationDO, Long oprator, SysInfo sysInfo) {

		IndividualDO artificialPersonDO = this.assemblyVo2ArtificialPersonDO(organizationVO, sysInfo);

		//保存法人的信息
		artificialPersonDO = this.addOrEditIndividual(artificialPersonDO);
		//把id返回去
		organizationVO.setCorporationId(artificialPersonDO.getParticipantId());

		//如果法人发生了改变
		if(!artificialPersonDO.getParticipantId().equals(organizationVO.getOldCorporationId())){

			//解除法人和企业的关系
			if(!BlankUtil.isBlank(organizationVO.getOldCorporationId())){
				this.deleteParticipantRelation(organizationDO.getParticipantId(), organizationVO.getOldCorporationId(), RelationTypeDO.RTYPE_ENTERPRISE_LEGAL);
			}
		}

		//建立企业和法人之间的关系
		this.addOrEditParticipantRelation(organizationDO, artificialPersonDO, RelationTypeDO.RTYPE_ENTERPRISE_LEGAL, oprator);
	}

	/** 功能描述: 给房屋有效银行账号设置字段值 <br>
	 * @param chvo
	 * @param proright
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void dealBankAccountInfo(CustomerHouseVO chvo, ProRightDO proright) {
		if (BlankUtil.isBlank(proright)) {
			return;
		}
		// 合并记账则查询管理房的产权
		if (SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(proright.getChargeSelect())) {
			CoalitionHouseDomain cHouse = this.houseDao.getCoalition(chvo.getHouseId());
			proright = this.proRightDao.getEffectiveProRightByHouseId(cHouse.getKeyword());
			if (BlankUtil.isBlank(proright)) {
				return;
			}
		}
		BankAccountVO baccvo = this.bankAccountService.getCurrentBankAccountByPro(proright.getProRightId());
		if (!BlankUtil.isBlank(baccvo)) {
			chvo.setAccountOwner(baccvo.getAccOwner());
			chvo.setBankId(baccvo.getBankId());
			chvo.setBankName(baccvo.getBankName());
			chvo.setBankNumber(baccvo.getAccNumber());
			chvo.setContactNumber(baccvo.getContactNumber());
		}
	}

	/**
	 * 功能描述: 代理人信息处理
	 *
	 * @author 付湘辉
	 * Created on：2008-6-20
	 *
	 * @author
	 * Update on：
	 * 修改原因：
	 *
	 * @param organizationVO
	 * @param organizationDO
	 * @param oprator
	 * @param sysInfo
	 */
	private void dealConsigner(OrganizationVO organizationVO, OrganizationDO organizationDO, Long oprator, SysInfo sysInfo) {

		IndividualDO agentDO = this.assemblyVO2ConsignerDO(organizationVO, sysInfo);

		//保存代理人的信息
		agentDO = this.addOrEditIndividual(agentDO);
		//把id返回去
		organizationVO.setConsignerId(agentDO.getParticipantId());

		//保存企业和代理人的之间的关系

         //如果代理人发生了改变
		if(!agentDO.getParticipantId().equals(organizationVO.getOldConsignerId())){

			//解除代理人和企业的关系
			if(!BlankUtil.isBlank(organizationVO.getOldConsignerId())){
				this.deleteParticipantRelation(organizationDO.getParticipantId(), organizationVO.getOldConsignerId(), RelationTypeDO.RTYPE_ENTERPRISE_PROXY);
			}
		}
		this.addOrEditParticipantRelation(organizationDO, agentDO, RelationTypeDO.RTYPE_ENTERPRISE_PROXY, oprator);
	}

	/** 功能描述:  <br>
	 * @param clienthouseDO
	 * @param customerHouseVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void dealHouseInfo(ClientRHouseDO clienthouseDO, CustomerHouseVO customerHouseVO) {
		HouseDomain houseDo = clienthouseDO.getHouse();
        Long houseId = houseDo.getKeyword();
        CoalitionHouseDomain  chouse = this.houseDao.getCoalition(houseDo);
		customerHouseVO.setHouseId(houseId);//房屋id
		customerHouseVO.setHouseNumber(houseDo.getHouseNumber());//房号
		customerHouseVO.setManageHouseId(chouse==null?null:chouse.getKeyword());
		//处理产权共有关系
        dealShareProRightHouse(customerHouseVO, houseDo);
	}

	/** 功能描述: 如果指定房屋与其房屋合并为产权共有关系，得到它们的id,放入参数customerHouseVO中 <br>
	 * @param customerHouseVO 客户房屋信息VO
	 * @param houseDo
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void dealShareProRightHouse(CustomerHouseVO customerHouseVO, HouseDomain houseDo) {
		CoalitionHouseDomain coalHouse = this.flatDao.getShareProRightCoalition(houseDo);
        if (!BlankUtil.isBlank(coalHouse)) {
            Set<HouseDomain> set = coalHouse.getHouses();
            for (HouseDomain domain : set) {
                Set<Long> tempSet = customerHouseVO.getProRightShareHouseIds() == null ? new HashSet<Long>() : customerHouseVO.getProRightShareHouseIds();
                tempSet.add(domain.getKeyword());
                customerHouseVO.setProRightShareHouseIds(tempSet);
            }
        }
	}


	private String getClientIds(MoveVO moveVO){
		if(moveVO == null) return null;

		StringBuffer clientds = new StringBuffer();
		for(Entry<Long,MoveVO.Possessor> etr: moveVO.getOwnerOrLessee().entrySet()) {
			clientds.append(etr.getKey())
			.append(",");

			for(Entry<Long,String> etre: etr.getValue().getMembers().entrySet()) {
				clientds.append(etre.getKey())
				.append(",");
			}
		}
		clientds.setLength(clientds.length() - 1);//去掉最后面的","
		return clientds.toString();
	}

	private String getHouseIds(MoveVO moveVO){
		if(moveVO == null) return null;
		StringBuffer houseIds = new StringBuffer();
		Set<Long> keys = moveVO.getHouses().keySet();

		for(Long id : keys){
			houseIds.append(id).append(",");
		}
		houseIds.setLength(houseIds.length() - 1);//去掉最后面的","
		return houseIds.toString();
	}

	/**
	 * 功能描述:如果输入的房号不存在，则返回真<br>
	 * @param vo
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private boolean isNotFoundHouseByHouseNumber(CustSearchVO vo) {
		return null!=vo.getHouseNumber() && vo.getHouseNumber().trim().length()>0 &&
				( null==vo.getHouseIds() || vo.getHouseIds().trim().length()<=0);
	}

}
